﻿using System; 
using System.Collections.Generic; 
using System.Data;
using Rogan.Common;
using Rogan.Common.Model;
using Rogan.Dal;

// ReSharper disable once CheckNamespace
namespace Rogan.Bll {
	 	//SysUser
		// ReSharper disable once PartialTypeWithSinglePart
	public partial class SysUserBll
	{
   		     
		private readonly SysUserDal _dal=new SysUserDal();


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int UserId)
		{
			return _dal.Exists(UserId);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void  Add(SysUser model)
		{
						_dal.Add(model);
						
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SysUser model)
		{
			return _dal.Update(model);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserId)
		{
			
			return _dal.Delete(UserId);
		}

		/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string UserIdlist )
		{
			return _dal.DeleteList(UserIdlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SysUser GetModel(int UserId)
		{
			return _dal.GetModel(UserId);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public SysUser GetModelByCache(int UserId)
		{
			
			string cacheKey = "Sys_UserModel-" + UserId;
			object objModel = DataCache.GetCache(cacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = _dal.GetModel(UserId);
					if (objModel != null)
					{
						int modelCache = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, objModel, DateTime.Now.AddMinutes(modelCache), TimeSpan.Zero);
					}
				}
				catch(Exception exception)
                {
                    LogHelper.WriteError("GetModelByCache",exception);
                }
			}
			return (SysUser)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataTable GetList(string strWhere)
		{
			return _dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataTable GetList(int top,string strWhere,string filedOrder)
		{
			return _dal.GetList(top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<SysUser> GetModelList(string strWhere)
		{
			DataTable dataTable = _dal.GetList(strWhere);
			return DataTableToList(dataTable);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<SysUser> DataTableToList(DataTable dt)
		{
			List<SysUser> modelList = new List<SysUser>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				for (int n = 0; n < rowsCount; n++)
				{
					SysUser model = _dal.DataRowToModel(dt.Rows[n]);
					
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataTable GetAllList()
		{
			return GetList("");
		}
	}
}


